<template>
  <div class="addgoods">
    <el-form :model="attribute.subsign" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="属性分类" prop="region">
        <el-select v-model="attribute.subsign.attribute_category_id" placeholder="请选择所属分类">
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="item in attribute.category"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="属性名称" prop="name">
        <el-input v-model="attribute.subsign.name"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
//引入vuex
import { mapState } from "vuex";

//引入API
import { postAddattributeInfo } from "@/request/api.js";

export default {
  methods: {
    submitForm() {
      //修改本地vuex
      this.$store.commit("attribute/goodsGetvalue", this.attribute.subsign);
      //发起请求
      this.$store.dispatch("attribute/goodsAddgoods");

      console.log("查看参数", this.attribute.subsign);
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    //初始化vuex
    let createds = {
      name: "",
      attribute_category_id: ""
    };
    this.$store.commit("categor/goodscreatedsubsign", createds);
    //初始化结束

    //获取分类数据
    this.$store.dispatch("attribute/attributeGetCategory");

    //判断是不是编辑内容
    if (this.$route.params.id) {
      postAddattributeInfo({
        id: this.$route.params.id
      }).then(res => {
        console.log("获取到商品详情", res.data);
        this.$store.commit("attribute/goodseditsubsign", res.data);
      });
    }
  },
  computed: {
    ...mapState(["categor"]),
    ...mapState(["attribute"])
  }
};
</script>

<style lang="less" scoped>
.addgoods {
  box-sizing: border-box;
  background-color: #fff;
  padding: 10px 0 0 20px;
  height: 95%;
  overflow-y: auto;
}
.addput011 {
  font-size: 12px;
  color: #999;
}

.el-select-dropdown__item.selected {
  color: #606266;
  font-weight: normal;
}
.el-input,
.el-textarea {
  width: 220px;
}

/deep/.avatar-uploader {
  border: 1px solid #d9d9d9;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 187px;
  height: 105px;
}

/deep/.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
/deep/.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 187px;
  height: 105px;
  line-height: 105px;
  text-align: center;
}

/deep/.avatar {
  width: 187px;
  height: 105px;
  display: block;
}
</style>